package com.westa.smallshop.goodsdetail;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;

import com.westa.smallshop.R;
import com.westa.smallshop.common.BaseActivity;
import com.westa.smallshop.data.source.remote.GoodsItemRemoteDataSource;
import com.westa.smallshop.util.ActivityUtils;

/**
 * Created by cy123 on 2016/9/14.
 */
public class GoodsDetailActivity extends BaseActivity {

    public static final String EXTRA_GOODS_ID = "goods_id";
    private int mGoodsId;

    public static void startActivity(Activity activity, int goodsId) {

        Intent intent = new Intent(activity, GoodsDetailActivity.class);
        intent.putExtra(EXTRA_GOODS_ID, goodsId);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_detail);

        GoodsDetailFragment goodsDetailFragment = (GoodsDetailFragment) getSupportFragmentManager()
                .findFragmentById(R.id.fragment_layout);

        if (goodsDetailFragment == null) {
            goodsDetailFragment = GoodsDetailFragment.newInstance();
            ActivityUtils.addFragmentToActivity(getSupportFragmentManager(),
                    goodsDetailFragment, R.id.fragment_layout);

        }

        new GoodsDetailPresenter(goodsDetailFragment, new GoodsItemRemoteDataSource(getApplicationContext()), mGoodsId);
    }

    @Override
    protected void onInitInstanceStateFromIntent() {
        super.onInitInstanceStateFromIntent();
        mGoodsId = getIntent().getIntExtra(EXTRA_GOODS_ID, 0);
    }

    @Override
    protected void onRestoreInstanceStateFromBundle(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceStateFromBundle(savedInstanceState);
        mGoodsId = savedInstanceState.getInt(EXTRA_GOODS_ID, 0);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt(EXTRA_GOODS_ID, mGoodsId);
    }
}
